# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

pkgname=tftp-hpa
pkgver=5.2
pkgrel=1
pkgdesc="Official tftp server"
arch=('i686' 'x86_64')
url="http://www.kernel.org/pub/software/network/tftp/tftp-hpa/"
license=('BSD')
groups=('base')
depends=('libreadline>=6.0.00')
makedepends=('libreadline-devel' 'gcc')
source=(http://www.kernel.org/pub/software/network/tftp/tftp-hpa/$pkgname-$pkgver.tar.xz
        LICENSE
        01-rename-server.patch
        02-missing-header.patch
        03-silence-warnings.patch
        04-cygwin-select-on-nonblocking-works.patch
        05-fix-ipv6-logic-error.patch
        tftp-5.2-1.src.patch
        tftp-hpa-0.49-fortify-strcpy-crash.patch
        msysize.patch
        )
md5sums=('e86f58164166804e8ae5a46010f29317'
         '6ce21e27b6fdc1a1adf85c81e42aeecf'
         '92aa71418b8707e7b07d0486f8a62009'
         'e6d98e521fc557b1d4d4fe72bb4a4d16'
         'cf98e700a1c98c52da8591972ee3ec42'
         '2781bc04858427b1c2d070c6823ef192'
         '784be996ecbd1003c644cb562f38bc51'
         'e28e9be3190c0d087f2cddd7d4eaa911'
         '22e8629ef19bc276a102c5d4d284c1bd'
         'f95cac393f4e6db390b8f7344da6c25c')

prepare() {
  cd ${pkgname}-${pkgver}

  # copy config.guess and config.sub, to support
  # the use of AC_CANONICAL_HOST in configure
  cp /usr/share/automake-1.11/config.guess ${srcdir}/${pkgname}-${pkgver}
  cp /usr/share/automake-1.11/config.sub ${srcdir}/${pkgname}-${pkgver}
  chmod +x ${srcdir}/${pkgname}-${pkgver}/config.guess
  chmod +x ${srcdir}/${pkgname}-${pkgver}/config.sub

  patch -Np2 -i ${srcdir}/01-rename-server.patch
  patch -Np2 -i ${srcdir}/02-missing-header.patch
  patch -Np2 -i ${srcdir}/03-silence-warnings.patch
  patch -Np2 -i ${srcdir}/04-cygwin-select-on-nonblocking-works.patch
  patch -Np2 -i ${srcdir}/05-fix-ipv6-logic-error.patch
  patch -Np2 -i ${srcdir}/tftp-5.2-1.src.patch

  # fix #28103
  patch -Np1 -i ${srcdir}/tftp-hpa-0.49-fortify-strcpy-crash.patch

  patch -Np1 -i ${srcdir}/msysize.patch

  autoreconf -fiv
}

build() {
  cd ${pkgname}-${pkgver}
  ./configure --prefix=/usr \
    --mandir=/usr/share/man \
    --sbindir=/usr/bin \
    --without-tcpwrappers \
    ac_cv_header_winsock_h=no \
    ac_cv_header_winsock2_h=no

  make
}

package() {
  cd ${pkgname}-${pkgver}
  make INSTALLROOT="${pkgdir}" install
  install -D -m644 "${srcdir}/LICENSE" "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  # Remove server files
  rm "${pkgdir}/usr/bin/tftpd.exe"
  rm "${pkgdir}/usr/share/man/man8/tftpd.8"

  # Install tftp header
  install -Dm 644 headers/arpa/tftp.h ${pkgdir}/usr/include/arpa/tftp.h
}
